package com.wzu.rjgc_ks.controller;

import com.wzu.rjgc_ks.dao.CompanyMapper;
import com.wzu.rjgc_ks.dao.JobMapper;
import com.wzu.rjgc_ks.dao.NoticeMapper;
import com.wzu.rjgc_ks.pojo.Company;
import com.wzu.rjgc_ks.pojo.Job;
import com.wzu.rjgc_ks.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexAction {

    @Autowired
    JobMapper jobMapper;
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    CompanyMapper companyMapper;


    @RequestMapping("/index")
    public String indexInfo(ModelMap modelMap, HttpServletRequest request){

        int noticeCount = noticeMapper.selectNoticeCount();
        int companyCount = companyMapper.selectComCount();

        List<Job> jobList = jobMapper.selectAll();
        modelMap.addAttribute("jobInfos",jobList);
        request.getSession().setAttribute("noticeCount",noticeCount);
        request.getSession().setAttribute("companyCount",companyCount);
        //modelMap.addAttribute("noticeCount",noticeCount);

        return "index";
    }

    @RequestMapping("/jobSearch")
    public ModelAndView searchJob(@RequestParam("jobName")String jobName,@RequestParam("address")String jobAddress){
        ModelAndView mv = new ModelAndView();

        List<Job> jobList = jobMapper.selectJobBySearch(jobName,jobAddress);
        System.out.println(jobList);
        mv.addObject("searchJobResults",jobList);
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/job/{job_id}")
    public ModelAndView jobDetailInfo(@PathVariable Integer job_id){
        ModelAndView mv = new ModelAndView();
        System.out.println(job_id);

        List<Job> jobDetailList = jobMapper.selectDetailByJobId(job_id);
        System.out.println(jobDetailList.toString());
        mv.addObject("jobDetailInfo",jobDetailList);
        mv.setViewName("detail_info");
        return mv;
    }

    @RequestMapping("/company")
    public  ModelAndView companyInfo(){
        ModelAndView mv = new ModelAndView();

        List<Company> CompanyList = companyMapper.selectAll();
        mv.addObject("companyInfos",CompanyList);
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/company/{company_id}")
    public  ModelAndView companyDetailInfo(@PathVariable Integer company_id){
        ModelAndView mv = new ModelAndView();
        Company company = companyMapper.selectByPrimaryKey(company_id);

        mv.addObject("CompanyDetailInfo",company);
        mv.setViewName("detail_info");
        return mv;
    }

    @RequestMapping("/notice")
    public ModelAndView noticeInfo(){
        ModelAndView mv = new ModelAndView();

        List<Notice> noticeList = noticeMapper.selectAll();
        mv.addObject("noticeInfos", noticeList);
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("/notice/{notice_id}")
    public ModelAndView noticeDetailInfo(@PathVariable Integer notice_id){
        ModelAndView mv = new ModelAndView();

        Notice noticeDetailList = noticeMapper.selectByPrimaryKey(notice_id);

        mv.addObject("noticeDetailInfo",noticeDetailList);
        mv.setViewName("detail_info");
        return mv;
    }

    @RequestMapping("/jobFilter")
    public  ModelAndView filterJob(@RequestParam(value = "jobName",required = false)String jobName,@RequestParam(value = "address",required=false) String address,@RequestParam(value = "edu",required = false) String edu,@RequestParam(value = "salary",required = false) String salary){
        ModelAndView mv = new ModelAndView();

        Map<String,Object> params = new HashMap<String,Object>();
        System.out.println(address + "  " + edu + "  " + salary);

        if(jobName != " " && jobName != null && !jobName.equals("null") && !jobName.equals("undefined")){
            params.put("jobName",jobName);
        }

        if(address != " " && address != null && !address.equals("null") && !address.equals("undefined")){
            params.put("address",address);
        }

        if(edu != " " && edu != null && !edu.equals("null") && !edu.equals("undefined")){
            String[] eduArr = edu.split(",");
            params.put("edu",eduArr);
        }

        if(salary != " " && salary != null && !salary.equals("null") && !salary.equals("undefined")){
            params.put("salary",salary);
        }

        List<Job> jobList = jobMapper.filterJob(params);
        mv.addObject("filterJobs",jobList);

        mv.setViewName("index");

        return mv;
//        switch(salary){
//            case "不限":
//                min_salary = 0;
//                break;
//            case "3k以下":
//                min_salary = 0;
//                max_salary = 3;
//                break;
//            case "3-5K":
//                min_salary = 3;
//                max_salary = 5;
//                break;
//            case "5-10K":
//                min_salary = 5;
//                max_salary = 10;
//                break;
//            case "10-15K":
//                min_salary = 10;
//                max_salary = 15;
//                break;
//            case "15-20K":
//                min_salary = 15;
//                max_salary = 20;
//                break;
//            case "20-30K":
//                min_salary = 20;
//                max_salary = 30;
//                break;
//            case "30-50K":
//                min_salary = 30;
//                max_salary = 50;
//                break;
//            case "50K以上":
//                min_salary = 50;
//                break;
//        }
//        switch (edu){
//            case "不限" :
//                break;
//            case "大专":
//                break;
//            case "本科" :
//                break;
//            case "硕士":
//                break;
//            case "博士" :
//                break;
//        }
    }

}
